<?php

namespace app\admin\middleware;

use app\admin\common;
use app\admin\model\index\Admin;

class CheckAuth
{
    public function handle($request, \Closure $next)
    {
        $token = $request->header()['token']??'';
        if(empty($token)){
            return common::return_json_layui([], '请登录',-1000);
        }
        $token_arr = explode('|',base64_decode($token));
        $admin_id = $token_arr[1];
        $admin = Admin::where('id',$admin_id)->find();
        if(time()>$admin['token_expiration']){
            return common::return_json_layui([], '登录已过期',-1000);
        }
        $tokenMd5=(new common())->tokenMd5($admin['token'],$token_arr[1],$token_arr[2]);
        if($tokenMd5!=$token_arr[0]){
            return common::return_json_layui([], 'token错误',-1000);
        }
        return $next($request);
    }
}